package com.example.sjly.ui.scheduling;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.sjly.MainActivity;
import com.example.sjly.R;
import com.example.sjly.db.LoginUser;
import com.example.sjly.ui.scheduling.room.TOrderIn;
import com.example.sjly.ui.scheduling.room.TOrderMess;
import com.example.sjly.ui.scheduling.room.TOrderRoomDatabase;

import java.util.LinkedList;
import java.util.List;

public class Schedulingmessgather extends AppCompatActivity implements View.OnClickListener {
    private ListView list_one;
    private SchedulingMyAdapter mAdapter = null;
    private List<Data> mData = null;
    private Context mContext = null;
    private TextView txt_empty;
    private ImageButton btn_add;
    private ImageButton btn_delete;
    private int delete_flag = 1;
    private int flag = 1;
    private Data mData_5 = null;//用来临时放对象的


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_scheduling_messagegather);
        mContext = Schedulingmessgather.this;
        bindViews();
        mData = new LinkedList<Data>();
        mAdapter = new SchedulingMyAdapter((LinkedList<Data>) mData, mContext);
        list_one.setAdapter(mAdapter);


        TOrderRoomDatabase tOrderRoomDatabase = TOrderRoomDatabase.getInstance(getApplicationContext());
        List<TOrderIn> tOrderIn = tOrderRoomDatabase.torderinDAO().getAll();
        for (int i = 0; i < tOrderIn.size(); i++) {
            TOrderIn tOrderIn1 = tOrderIn.get(i);
            mData_5 = new Data(R.mipmap.ic_launcher, tOrderIn1.getTeacherName(), tOrderIn1.getTeacherPhone(), tOrderIn1.getAddressMess() + flag);
            mAdapter.add(mData_5);
            flag++;
        }
    }

    private void bindViews() {
        txt_empty = (TextView) findViewById(R.id.txt_empty);
        list_one = (ListView) findViewById(R.id.messListView);
        btn_add = (ImageButton) findViewById(R.id.btn_add);//添加按钮
        btn_delete = (ImageButton) findViewById(R.id.btn_delete);//删除按钮

        //页面显示(隐藏页面——若是没有数据则显示该文字)
        txt_empty.setText("请添加数据~");
        list_one.setAdapter(mAdapter);
        list_one.setEmptyView(txt_empty);

        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    }


    private void updateListItem(int postion, Data mData) {
        int visiblePosition = list_one.getFirstVisiblePosition();
        View v = list_one.getChildAt(postion - visiblePosition);

        ImageView img = (ImageView) v.findViewById(R.id.messgather_s_peopleicon);
        TextView tv1 = (TextView) v.findViewById(R.id.messgather_s_name);
        TextView tv2 = (TextView) v.findViewById(R.id.messgatherphone);
        TextView tv3 = (TextView) v.findViewById(R.id.messgather_s_address);

        img.setImageResource(mData.getImgId());

        tv1.setText(mData.getContent());
        tv2.setText(mData.getContent());
        tv3.setText(mData.getContent());
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_add://添加按钮

                //点击添加页面跳转  
                Intent intent = new Intent(Schedulingmessgather.this, Schedulingmessgathnext.class);
                startActivityForResult(intent, 0);

                break;
            case R.id.btn_delete://删除按钮
                //position从0开始算的
                TOrderRoomDatabase tOrderRoomDatabase = TOrderRoomDatabase.getInstance(getApplicationContext());
                List<TOrderIn> TOrderin = tOrderRoomDatabase.torderinDAO().getAll();
                int flag = TOrderin.size();
                if (flag == 0) {
                    Toast.makeText(getApplicationContext(), "后台已无数据", Toast.LENGTH_SHORT).show();
                } else {
                    delete_tOrder(delete_flag++);//删第一个
                    mAdapter.remove(1);
                }
                break;

        }
    }

    private void delete_tOrder(int i) {
        TOrderRoomDatabase tOrderRoomDatabase = TOrderRoomDatabase.getInstance(getApplicationContext());
        TOrderIn tOrderIn = tOrderRoomDatabase.torderinDAO().getByID(i);
        tOrderRoomDatabase.torderinDAO().delete(tOrderIn);
        Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
    }


    @Override
    public void onResume(){
        super.onResume();
        refreshData();
    }

    private void refreshData() {
        try {
            TOrderRoomDatabase tOrderRoomDatabase = TOrderRoomDatabase.getInstance(getApplicationContext());
            List<TOrderIn> tOrderIn = tOrderRoomDatabase.torderinDAO().getAll();
            for (int i = 0; i < tOrderIn.size(); i++) {
                TOrderIn tOrderIn1 = tOrderIn.get(i);
                mData_5 = new Data(R.mipmap.ic_launcher, tOrderIn1.getTeacherName(), tOrderIn1.getTeacherPhone(), tOrderIn1.getAddressMess() + flag);
                mAdapter.add(mData_5);
                flag++;
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }
    }

    /*@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


//    if(flag == 5){ //数据库暂未建立，先用已有数据代替
            TOrderRoomDatabase tOrderRoomDatabase=TOrderRoomDatabase.getInstance(getApplicationContext());
            List<TOrderIn> tOrderIn=tOrderRoomDatabase.torderinDAO().getAll();
        for(int i=0; i<tOrderIn.size(); i++){
            TOrderIn tOrderIn1=tOrderIn.get(i);
            mData_5 = new Data(R.mipmap.ic_launcher,tOrderIn1.getTeacherName() ,tOrderIn1.getTeacherPhone(),tOrderIn1.getAddressMess()+flag);
            mAdapter.add(mData_5);
            flag++;
        }*/

//        }else{
//            mAdapter.add(new Data(R.mipmap.ic_launcher,"郑淇淇" ,"17722100495","广东省汕头市潮阳区~"+flag));
//      }

    }
//}